package com.wang.kmp.shared.server.exts

import com.wang.kmp.shared.data.base.BaseResp
import com.wang.kmp.shared.data.base.BaseTypeResp
import io.ktor.server.application.ApplicationCall
import io.ktor.server.response.respond
import kotlinx.serialization.json.JsonElement

suspend inline fun ApplicationCall.respondSuccess() {
    respond(BaseResp(BaseResp.CODE_SUCCESS))
}

suspend inline fun <reified T> ApplicationCall.respondSuccess(data: T) {
    respond(BaseTypeResp(BaseResp.CODE_SUCCESS, data = data))
}

/**
 * 将data当成json对象返回
 */
suspend inline fun ApplicationCall.respondSuccessDataJson(dataJson: String) {
    respondSuccess(JsonMy.decodeFromString<JsonElement>(dataJson))
}

suspend inline fun ApplicationCall.respondFailed(code: Int, message: String, debugMessage: String = "") {
    respond(BaseResp(code, message, debugMessage))
}